package com.mcr.graduateprogram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.Manager;
import com.mcr.graduateprogram.common.R;
import com.mcr.graduateprogram.service.IManagerService;
import com.mcr.graduateprogram.service.IPhotoUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Api(tags = "管理员相关接口")
@CrossOrigin
@RestController
@RequestMapping("/graduateprogram/manager")
public class ManagerController {
    @Autowired
    private IManagerService iManagerService;
    @Autowired
    private IPhotoUpload iPhotoUpload;
    @Value("${qiniu.manager}")
    private String managerPhoto;
    @ApiOperation(value = "分页查询管理员账号")
    @GetMapping("getPage")
    private R getByPage(@RequestParam(value = "page")Integer page,
                        @RequestParam(value = "pageSize")Integer pageSize,
                        @RequestParam(value = "name",required = false)String name){
        Page<Manager> managerPage = iManagerService.getPage(page, pageSize,name);
        return R.success(managerPage);
    }
    @ApiOperation(value = "根据id删除单个管理员账号")
    @GetMapping("delById")
    private R delById(@RequestParam(value = "id")Long id){
        iManagerService.removeById(id);
        return R.success(null);
    }
    @ApiOperation(value = "根据ids批量删除管理员账号")
    @GetMapping("delByIds")
    private R delByIds(@RequestParam(value = "ids")List<Long> ids){
        iManagerService.removeBatchByIds(ids);
        return R.success(null);
    }
    @ApiOperation(value = "管理员添加")
    @PostMapping("addManager")
    private R addManager(@RequestBody Manager manager){
        return R.success(iManagerService.addManager(manager));
    }
    @ApiOperation(value = "管理员修改")
    @PostMapping("updateManager")
    private R updateManager(@RequestBody Manager manager){
        return R.success(iManagerService.updateManagerById(manager));
    }
    @ApiOperation(value = "上传管理员头像")
    @PostMapping("upload")
    public R upload(@RequestPart("photo") MultipartFile photo){
        return R.success(iPhotoUpload.photoUpload(photo,managerPhoto));
    }
    @ApiOperation(value = "管理员登录")
    @PostMapping("login")
    public R login(@RequestBody Manager manager){
        return R.success(iManagerService.login(manager));
    }
}
